#pragma once
#ifndef _SERVER_MAIN_HPP_
#define _SERVER_MAIN_HPP_
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include <sys/epoll.h>
#include <string.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <signal.h>
#include <map>
#include <ctype.h>
#include <functional>
#include "cJSON.h"

#define BUF_SIZE 512
#define MAX_EVENTS 64
#define SERVER_PORT 9999
#define SERVER_ADDRESS "127.0.0.1"
#define FILE_NAME "test.json"
#define CLIENT_REGISTER_MESSAGE "Welcome,maybe we should know your name"

#define ERROR_FUNC(fd,str)  if( (fd) ) { perror( (str) ); abort();}
#define ERROR_FUNC_PRINT(fd,str) if( (fd) ) { perror( (str) ); return -1;}

namespace BaseLib
{
	enum class CLIENT_TYPE
	{
		NONE = 0,
		REGISTER = 1,
		CHAT = 2,
		LOGOUT = 3,
	};

	typedef struct _tCLIENTDATA
	{
		CLIENT_TYPE type;
		char* name;
		int fd;
	}ClientData, * pClientData;
	
	/*创建并绑定socket*/
	int create_and_bind_socket();
	/*设置非阻塞*/
	int set_noblocking(int sockfd);
	/*添加epoll事件*/
	int add_epoll(int epollfd,int fd);
	/*广播消息*/
	int broadcase_message(int fd, std::map<int,ClientData> client_list, char* buf, int size);
	/*创建服务器文件*/
	int create_file_for_state(const char* filename,char** content);
	/*获取所有文件内容*/
	int get_file_all(const char* filename, char** content);
	/*询问服务器状态*/
	int ask_server_for_state(const char* str, int fd, const char* name,ClientData& data);
	/*将文件保存为json格式*/
	int save_data_as_json(char** str, const ClientData& client);
	/*将内容保存到本地*/
	int save_data_in_file(const char* filename, char* content);
	/*转换为大写*/
	int to_up_word(char* content);
	/*信号处理函数*/
	int add_sig(int sig,void(*func)(int));
}

#endif /*_SERVER_MAIN_HPP_*/